﻿using github.heerheer.card2.UI.ViewModels;
using HeerCards;
using LitJson;
using Panuon.UI.Silver;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;

namespace github.heerheer.card2.UI.Views
{
    /// <summary>
    /// PoolEditorView.xaml 的交互逻辑
    /// </summary>
    public partial class PoolEditorView : UserControl
    {
        public PoolEditorViewModel PoolEditorViewModel { get; set; }

        public PoolEditorView()
        {
            InitializeComponent();
            this.DataContext = PoolEditorViewModel = new PoolEditorViewModel();
        }

        private void Btn_EditPool(object sender, RoutedEventArgs e)
        {
            string name = (string)((Button)sender).Tag;
            var a = new PoolContentView(name).ShowDialog();
            PoolEditorViewModel = new PoolEditorViewModel();
        }

        private void Method_SavePools()
        {
            foreach (var item in PoolEditorViewModel.Pools)
            {
                App.SavePool(item);
            }
            MessageBoxX.Show("保存成功了！", "保存");
        }



        private void Btn_AddPool(object sender, RoutedEventArgs e)
        {
            InputDialog inp = new InputDialog("输入卡池名称");
            var result = inp.ShowDialog();
            if (result == true && inp.Value != string.Empty)
            {
                PoolEditorViewModel.Pools.Add(new HCPool() { PoolName = inp.Value });
                MessageBoxX.Show("添加成功", "卡池");
                if (inp.Value.Contains("崩坏"))
                {
                    //触发提示
                    //https://cqp.cc/t/47221
                    var r = MessageBoxX.Show("我们推荐崩坏三卡池使用另一个插件\n点击确定跳转", "跳转", null, MessageBoxButton.YesNo);
                    if (r == MessageBoxResult.Yes)
                    {
                        Process.Start(@"https://cqp.cc/t/47221");
                    }
                }
            }
            Method_SavePools();

        }
        private void Btn_DeletePool(object sender, RoutedEventArgs e)
        {
            HCPool name =(HCPool)((Button)sender).Tag;
            
            bool v = PoolEditorViewModel.Pools.Remove(name);
            Method_SavePools();
            File.Delete(App.getPoolPath(name.PoolName));

        }
        private async void Btn_CheckError(object sender, RoutedEventArgs e)
        {
            StringBuilder sb = new StringBuilder();
            string name = (string)((Button)sender).Tag;
            var ind = new List<HCPool>(PoolEditorViewModel.Pools).Find(s => s.PoolName == name);
            var iph = PendingBox.Show("正在为你分析","卡池差错",false,MainWindow.mainWindow);
            await Task.Delay(500);
            iph.UpdateMessage("分析等级占比数据...");
            await Task.Factory.StartNew(() =>
            {
                int r0 = 0;
                foreach ( var item in ind.LevelCollections)
                {

                    if (item.Ratio <= 0)
                    {
                        r0++;
                        sb.AppendLine($"等级{item.Name}的占比为0");
                    }
                }
                if (r0 == ind.LevelCollections.Count)
                {
                    sb.AppendLine($"[严重问题]所有等级的占比都为0,无法进行抽卡");
                }
            });
            iph.UpdateMessage("分析卡池设置...");
            await Task.Delay(500);

            await Task.Factory.StartNew(() =>
            {
                if(ind.Message=="" || ind.Message == null)
                {
                    sb.AppendLine($"[严重问题]卡池触发语句为空");
                }
            });
            iph.UpdateMessage("分析图片开关设置...");
            await Task.Delay(500);

            await Task.Factory.StartNew(() =>
            {
                if (ind.useImage == false)
                {
                    sb.AppendLine($"[图片相关]卡池图片开关被关闭");
                }
            });
            iph.UpdateMessage("分析全局设置...");
            await Task.Factory.StartNew(() =>
            {
                sb.AppendLine($"代码调整中...");
            });
            await Task.Delay(500);

            iph.UpdateMessage("分析完成!\n生成数据中");
            await Task.Delay(1000);
            iph.Close();
            MessageBox.Show(sb.ToString());


        }
        private void Btn_Refresh(object sender,RoutedEventArgs e)
        {
            this.DataContext = PoolEditorViewModel = new PoolEditorViewModel();
        }
    }
}